package j.framework.tags.form;

import j.framework.core.DictItem;
import j.framework.core.FApplication;
import j.framework.tags.TagUtils;
import j.util.StringUtils;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.util.List;

/**
 * code与items只能选其一
 * User: jason
 * Date: 2014/11/7 17:13
 */
@Deprecated
public class SelectTag1 extends SimpleTagSupport {
    private String name;
    private String code;
    private List<DictItem> items;
    private String onchange;
    private String selectedValue;
    private String theFirstItemText = "请选择...";
    private String cssClass;
    private boolean isRequired = false;//是否必填项

    @Override
    public void doTag() throws JspException, IOException {
        if(StringUtils.isNullOrEmpty(code) &&
                items == null){
            throw new RuntimeException("SelectTag 必须指定code或items其中一项，若都指定，则以items为主！");
        }
        List<DictItem> list = this.items;
        if(null == list){
            list = FApplication.getInstance().getDictItems(code);
        }
        String s = TagUtils.generateDictItemsForWebSelect(name, list,
                onchange, selectedValue, theFirstItemText, cssClass, isRequired);
        getJspContext().getOut().write(s);
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public void setItems(List<DictItem> items) {
        this.items = items;
    }

    public void setOnchange(String onchange) {
        this.onchange = onchange;
    }

    public void setSelectedValue(String selectedValue) {
        this.selectedValue = selectedValue;
    }

    public void setTheFirstItemText(String theFirstItemText) {
        this.theFirstItemText = theFirstItemText;
    }

    public void setCssClass(String cssClass) {
        this.cssClass = cssClass;
    }

    public void setRequired(boolean required) {
        isRequired = required;
    }
}
